package com.online.fund.control;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.online.fund.spider.entity.Person;
import com.online.fund.util.DateUtils;
import com.online.fund.util.ExcelUtils;

import cn.afterturn.easypoi.excel.entity.ExportParams;

@Controller
public class PersonControl {

	@RequestMapping("/export")
	public void export(HttpServletResponse response) {

		// 模拟从数据库获取需要导出的数据
		List<Person> personList = new ArrayList<>();
		Person person1 = new Person("路飞", "1", new Date());
		Person person2 = new Person("娜美", "2", DateUtils.addDate(new Date(), 3));
		Person person3 = new Person("索隆", "1", DateUtils.addDate(new Date(), 10));
		Person person4 = new Person("小狸猫", "1", DateUtils.addDate(new Date(), -10));
		personList.add(person1);
		personList.add(person2);
		personList.add(person3);
		personList.add(person4);

		// 导出操作
		ExcelUtils.exportExcel(personList, "花名册", "草帽一伙", Person.class, "海贼王.xls", response);
	}
	
	@RequestMapping("/export2")
	public void export2(HttpServletResponse response) {

		// 模拟从数据库获取需要导出的数据
		List<Person> personList = new ArrayList<>();
		Person person1 = new Person("路飞", "1", new Date());
		Person person2 = new Person("娜美", "2", DateUtils.addDate(new Date(), 3));
		Person person3 = new Person("索隆", "1", DateUtils.addDate(new Date(), 10));
		Person person4 = new Person("小狸猫", "1", DateUtils.addDate(new Date(), -10));
		personList.add(person1);
		personList.add(person2);
		personList.add(person3);
		personList.add(person4);
		
		Map<String, Object> map1 = new HashMap<>();
		ExportParams params1 = new ExportParams(null, "测试1");
//		params1.setCreateHeadRows(false);
		map1.put("title", params1);
		map1.put("entity", Person.class);
		map1.put("data", personList);
		List<Map<String, Object>> list = new ArrayList<>();
		list.add(map1);
		
		List<Person> person2List = new ArrayList<>();
		person2List.add(new Person("测试bcd", "1", new Date()));
		
		Map<String, Object> map2 = new HashMap<>();
		ExportParams params2 = new ExportParams(null, "测试2");
//		params2.setCreateHeadRows(false);
		map2.put("title", params2);
		map2.put("entity", Person.class);
		map2.put("data", person2List);
		list.add(map2);
		
		// 导出操作
		ExcelUtils.exportExcel(list, "测试数据.xls", response);
	}

	@RequestMapping("/importExcel")
	public void importExcel() {
		String filePath = "F:\\海贼王.xls";
		// 解析excel，
		List<Person> personList = ExcelUtils.importExcel(filePath, 1, 1, Person.class);
		// 也可以使用MultipartFile,使用 FileUtil.importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
		// Class<T> pojoClass)导入
		System.out.println("导入数据一共【" + personList.size() + "】行");

		// TODO 保存数据库
	}

}
